package com.ezzd.service.impl;

import com.ezzd.EnmuInfo;
import com.ezzd.dao.RoleDao;
import com.ezzd.dao.UserDao;
import com.ezzd.pojo.TbUser;
import com.ezzd.service.UserService;
import com.ezzd.utils.RedisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description tb_user
 * @author 被遗忘的湫
 * @date 2022-07-31
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private UserDao userDao;
    @Resource
    private RoleDao roleDao;


    @Override
    public Object insert(TbUser tbUser) {

        // valid
        if (tbUser == null) {
            return EnmuInfo.Fail;
        }

        userDao.insert(tbUser);
        return EnmuInfo.Success;
    }


    @Override
    public Object delete(int id) {
        int ret = userDao.delete(id);
        return ret > 0 ? EnmuInfo.Success : EnmuInfo.Fail;
    }


    @Override
    public Object update(TbUser tbUser) {
        int ret = userDao.update(tbUser);
        return ret > 0 ? EnmuInfo.Success : EnmuInfo.Fail;
    }


    @Override
    public TbUser load(int id) {
        return userDao.load(id);
    }


    @Override
    public PageInfo<TbUser> pageList(int pageNum, int pagesize) {

        PageHelper.startPage(pageNum, pagesize);
        List<TbUser> pageList = userDao.pageList();

        PageInfo<TbUser> pageInfo = new PageInfo<>(pageList);

        return pageInfo;
    }

    @Override
    public TbUser queryUserAndRole(String userName,String password,String roleName) {

        TbUser tbUser = userDao.queryUserAndRole(userName,password,roleName);
         return tbUser;
    }
}